import java.sql.*;

public class jdbcFind {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        jdbcFind find = new jdbcFind();
        // ---------------------Begin------------------------
        //获取数据库连接
        Connection conn = find.getConnection();

        //获取statement对象
        Statement stmt = conn.createStatement();

        //遍历输出staff表中的数据
        String sql = "SELECT id,name,sex,age FROM staff";
        ResultSet rs = stmt.executeQuery(sql);
        // 当下个元素仍存在
        while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            int age = rs.getInt("age");
            String sex = rs.getString("sex");
            // String department = rs.getString("department");
            // double salary = rs.getDouble("salary");

            System.out.println(id + "," + name + "," + sex + "," + age);
        }

        //关闭数据库连接，释放资源
        rs.close();
        stmt.close();
        conn.close();

    }

    // ---------------------End------------------------
    public Connection getConnection() throws ClassNotFoundException, SQLException {
        // ---------------------Begin------------------------
        //获取数据库连接，账户名：root,密码：123123, 数据库名：company,MySQL版本为8.0.20
        Class.forName("com.mysql.cj.jdbc.Driver");
        String username = "root";
        String password = "123123";
        String url = "jdbc:mysql://localhost:3306/company?serverTimezone=UTC&useSSL=false";
        return DriverManager.getConnection(url, username, password);
        // ---------------------End------------------------
    }
}